package com.atguigu.gmall.list.listener;

import com.atguigu.gmall.common.constant.MqConst;
import com.atguigu.gmall.list.service.ListService;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * @author: 李旭
 * @create: 2021-09-13 10:56
 *
 *   监听器 接收消息
 *     .xml  SSM        Spring发展史 第一阶段   第二阶段 注解开发  第三阶段 SpringBoot Java代码配置
 *       springmvc.xml
 *     .properties  .yml  SpringBoot  Java代码配置
 **/
@Component
public class ListListener {

    @Autowired
    private ListService listService;

    //上架的
    @RabbitListener(bindings = {
            @QueueBinding(
                    value = @Queue(value = MqConst.QUEUE_GOODS_UPPER,durable = "true",autoDelete = "false"),
                    exchange = @Exchange(value = MqConst.EXCHANGE_DIRECT_GOODS),
                    key = {MqConst.ROUTING_GOODS_UPPER}
            )
    })
    public void onSale(Long skuId, Channel channel, Message message){
        try {
            System.out.println("skuId:" + skuId);
            //上架
            listService.onSale(skuId);
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
    //下架的
    @RabbitListener(bindings = {
            @QueueBinding(
                    value = @Queue(value = MqConst.QUEUE_GOODS_LOWER,durable = "true",autoDelete = "false"),
                    exchange = @Exchange(value = MqConst.EXCHANGE_DIRECT_GOODS),
                    key = {MqConst.ROUTING_GOODS_LOWER}
            )
    })
    public void cancelSale(Long skuId, Channel channel, Message message){
        try {
            System.out.println("skuId:" + skuId);
            //
            listService.onCancel(skuId);
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}
